<?php
require('../database/db.php');
$mysql = new mysql('xzsh');
include("../../tool/UpFileTool.class.php");
error_reporting(0);

/*if ($_FILES) {
	$UpFileTool=new UpFileTool('upfile');
	$UpFileTool->multiImage($w=500, $h=500,$sumbimage='../../sumbimage', $srcimage='../../srcimage');
	$images=$UpFileTool->multiSumbImage;
	$srcimages=$UpFileTool->multiSrcImage;
}*/
// 设置主图

// var_dump($GLOBALS["HTTP_RAW_POST_DATA"]);die;
// $res = $GLOBALS["HTTP_RAW_POST_DATA"];
// $_REQUEST = json_decode($GLOBALS["HTTP_RAW_POST_DATA"], true);

if ($_REQUEST['num'] != '') {
	$num = $_REQUEST['num'];
}else{
	$num = 0;
}
// 设置主图 end

// 设置详情图主图
if ($_REQUEST['defaultXQTuOrd'] != '') {
	$defaultXQTuOrd = $_REQUEST['defaultXQTuOrd'];
}else{
	$defaultXQTuOrd = 0;
}
//echo json_encode(array('1' =>$num,'123' => $defaultXQTuOrd));exit();
// 设置详情图主图 end

// $images = implode(',',$imgname);
//$der=array('im'=>$images);

if ($_REQUEST['goodsid'] != '') {
	$sql="select * from goods where goodsid = ".$_REQUEST['goodsid'];
	$goods = $mysql->getRow($sql);
	if ($goods) {
		
		$mode = 'update';
		$where = ' where goodsid='.$_REQUEST['goodsid'];

	}else{
		$mode ='insert';
		$where = ' where 1 limit 1';
	}
}else{
	$mode ='insert';
	$where = ' where 1 limit 1';
}
//查询shopid
$sql = "SELECT shopid FROM shop WHERE phone = " . $_REQUEST['phone'];
$shop = mysql_fetch_assoc(mysql_query($sql));
//end

$res = json_decode($_REQUEST['guige'],true);
$shopid         = $shop['shopid'];
$name           = trim($_REQUEST['name']);
$shelves        = trim($_REQUEST['shelves']);
$price          = trim($_REQUEST['price']);
$original       = trim($_REQUEST['original']);
$can_fanquan    = trim($_REQUEST['can_fanquan']);
$son_class      = trim($_REQUEST['son_class']);
$guige          = trim($_REQUEST['guige']);
$images         = trim($_REQUEST['images']);
$srcimages      = trim($_REQUEST['srcimages']);
$xiangQingTu    = trim($_REQUEST['xiangQingTu']);
$srcXiangQingTu = trim($_REQUEST['srcXiangQingTu']);
$start_num      = trim($_REQUEST['start_num']);
$Freight_dir    = trim($_REQUEST['Freight_dir']);
$goods_dir      = trim($_REQUEST['goods_dir']);
$muid           = trim($_REQUEST['muid']);
$start_num_desc = trim($_REQUEST['start_num_desc']);
// $guige=explode(';',$guige);
// $length=count($res['data']);
$table = 'goods';
$arr = array(
	'title'          =>		$name,
	'shangjia'       =>		$shelves,
	'price'          =>		$price,
	'yuanjia'        =>		$original,
	'can_fanquan'    =>		'0',
	'zixilie'        =>		$son_class,
	'images'         =>		$images,
	'srcimages'      =>		$srcimages,
	'defaultImgOrd'  =>		$num,
	'xiangQingTu'    =>		$xiangQingTu,
	'srcXiangQingTu' =>		$srcXiangQingTu,
	'defaultXQTuOrd' =>		$defaultXQTuOrd,
	'start_num'      =>		$start_num,
	'start_num_desc' =>		$start_num_desc,
	'Freight_dir'    =>		$Freight_dir,
	'goods_dir'      => 	$goods_dir,
	'muid'           =>		$muid,
	'shopid'         =>		$shopid
);

$we = $mysql->autoExecute($table, $arr, $mode, $where);
if ($mode == 'update') {
	$result = $_REQUEST['goodsid'];
}else{
	$result = $mysql->insert_id();
}
$sql="DELETE from goodsselect where goodsid = ". $result;
$de = $mysql->query($sql);
/*for($i=0;$i<count($res);$i++) {
	$num = 
	for($i=0;$i<$length;$i++){
		$ar = $res[$i]['data'][$i];
		$tab ='goodsselect';
		$ary = array(
			'goodsid'=>$result,
			'fTitle'=>$res['fTitle'],
			'sTitle'=>$ar['name'],
			'price'=>$ar['price'],
			'number'=>$ar['num']
		);

		$mysql->autoExecute($tab,$ary);
	}
}*/


// $result = '76155';
foreach($res as $rkey => $rvalue) {
	if(empty($res[$rkey]['data'])) {
		$tab ='goodsselect';
		$ary = array(
			'goodsid' => $result,
			'fTitle' => $rvalue['fTitle'],
			'price' => $rvalue['price'],
			'number' => $rvalue['num']
		);
		$lala = $mysql->autoExecute($tab,$ary);
	} else {
		foreach($res[$rkey]['data'] as $key => $value) {
			$tab ='goodsselect';
			$ary = array(
				'goodsid' => $result,
				'fTitle' => $rvalue['fTitle'],
				'sTitle' => $value['name'],
				'price' => $value['price'],
				'number' => $value['num']
			);
			$lala = $mysql->autoExecute($tab,$ary);
		}
	}

}
$result2 = $mysql->insert_id();
// var_dump($result);
// var_dump($result);
if($result != false && $result2 != false){
	$der=array('code'=>100);
}else{
	$der=array('code'=>200);
}

echo json_encode($der);
?>